package com.fh.rabbit;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
public class DeadSendMessageController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping
    public String sendMessage(String orderSn){
        String msgId = UUID.randomUUID().toString();
        Map<String,Object> map=new HashMap<>();
        map.put("sendTime",new Date());
        map.put("content",orderSn);
        rabbitTemplate.convertAndSend("orderTtlDirect", "orderTtlDirectRoutingKey", map,
                new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setExpiration(String.valueOf(60000));
                        return message;
                    }
                }, new CorrelationData(msgId));
        return "success";
    }
}
